package com.huayi.breakpoint_continuation_interface.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.huayi.breakpoint_continuation_interface.bean.BreakpointStats;
import com.huayi.breakpoint_continuation_interface.bean.User;
import com.huayi.breakpoint_continuation_interface.mapper.Breakpoint_Continuation_FromCk_Mapper;
import com.huayi.breakpoint_continuation_interface.service.Breakpoint_Continuation_FromCk_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description :
 * @Author:yujiahao
 * @Date: 2022/6/10 10:04
 * @Version: 1.0
 */
@Service
public class Breakpoint_Continuation_FromCk_ServiceImpl implements Breakpoint_Continuation_FromCk_Service {
   @Autowired
   private Breakpoint_Continuation_FromCk_Mapper breakpointContinuationFromCkMapper;

    @Override
    public List<BreakpointStats> getAll() {
        return breakpointContinuationFromCkMapper.selectAll();
    }

    @Override
    public User getAccount(User user) {
        return breakpointContinuationFromCkMapper.getAccount(user);
    }

    @Override
    public String getToken(User user) {
        Date start = new Date();
        long currentTime = System.currentTimeMillis() + 60* 60 * 1000;//一小时有效时间
        Date end = new Date(currentTime);
        String token = "";

        token = JWT.create().withAudience(user.getUser_name()).withIssuedAt(start).withExpiresAt(end)
                .sign(Algorithm.HMAC256(user.getPassword()));
        return token;
    }

    public User findByUsername(String username) {
        return breakpointContinuationFromCkMapper.findByUsername(username);
    }
}
